/*
 * @Author: chengjiang
 * @Date: 2025-04-17 19:36:15
 * @Description: 数字转中文大写数字工具
 */
export const numToChinessCap = (num: number | undefined) => {
  if (!num) return '';
  let numStr = num.toString();
  numStr = numStr.trim();
  console.log(`numStr = ${numStr}`);
  const regex = /^\d+(?:\.\d+)?$/;
  if (!regex.test(numStr)) {
    // console.log("不是有效的整数或小数");
    return '';
  }
  if (numStr.startsWith('.')) {
    // console.log("不是有效的整数或小数");
    return '';
  }
  if (numStr.endsWith('.')) {
    // console.log("不是有效的整数或小数");
    return '';
  }
  if (numStr.length > 1 && numStr.startsWith('0') && !numStr.startsWith('0.')) {
    // console.log("不是有效的整数或小数");
    return '';
  }
  const nums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
  const integerUtits = ['', '拾', '佰', '仟'];
  const parts = numStr.split('.');
  if (parts.length !== 1 && parts.length !== 2) {
    // console.log("不是有效的整数或小数");
    return '';
  }
  const integerPart = parts[0];
  if (!integerPart) {
    return '';
  }
  let chineseInteger = '';
  const chineseIntegerArray = [];
  let integerIndex = 0;
  for (let i = integerPart.length - 1; i >= 0; i--) {
    // 从后往前依次拿到单个数字
    const singleNum = integerPart[i];
    // 单个数字转换成中文大写汉字
    const chinessCapNum = nums[singleNum];
    // 该数字后面应该接的单位：個、拾、佰、仟(其中‘個’这个单位就省略掉了，不需要展示，用空字符串替代)
    const integerUtit = integerUtits[integerIndex % 4];
    // 该数字后面应该额外接的单位：萬或者亿，第5位开始加萬，第9位开始加亿，再往后每四位切换使用萬和亿
    let integerExtUtit = '';
    if (integerIndex !== 0) {
      if (integerIndex % 8 === 0) {
        integerExtUtit = '亿';
      } else if (integerIndex % 4 === 0) {
        integerExtUtit = '萬';
      }
    }
    chineseIntegerArray[integerIndex] =
      chinessCapNum + integerUtit + integerExtUtit;
    integerIndex++;
  }
  // console.log("chineseIntegerArray = ", chineseIntegerArray);
  for (let i = chineseIntegerArray.length - 1; i >= 0; i--) {
    chineseInteger += chineseIntegerArray[i];
  }
  // console.log("整数部分转大写汉字后的结果 = " + chineseInteger);
  let chineseDecimal = '';
  // 判断有没有小数，如果有，则需要对小数部分做处理
  if (parts.length === 2) {
    const decimalUnits = ['角', '分', '毫', '厘'];
    const decimalPart = parts[1];
    const valildCount = Math.min(decimalPart.length, 4);
    for (let i = 0; i < valildCount; i++) {
      chineseDecimal += nums[decimalPart[i]] + decimalUnits[i];
    }
      // console.log("小数部分转大写汉字后的结果 = " + chineseDecimal);
  }
  const fullChinessCap = `${chineseInteger}圆${chineseDecimal}`;
  // console.log("完整大写汉字 = " + fullChinessCap);
  let replacedChinessCap = fullChinessCap.replaceAll(/零[角分毫厘]/g, '');
  // console.log("replacedChinessCap 1 = " + replacedChinessCap);
  replacedChinessCap = replacedChinessCap.replace(/零[仟佰拾]/g, '零');
  // console.log("replacedChinessCap 2 = " + replacedChinessCap);
  replacedChinessCap = replacedChinessCap.replace(/零+/g, '零');
  // console.log("replacedChinessCap 3 = " + replacedChinessCap);
  replacedChinessCap = replacedChinessCap.replace(/零萬/g, '萬');
  // console.log("replacedChinessCap 4 = " + replacedChinessCap);
  replacedChinessCap = replacedChinessCap.replace(/零亿/g, '亿');
  // console.log("replacedChinessCap 5 = " + replacedChinessCap);
  replacedChinessCap = replacedChinessCap.replace(/萬萬/g, '亿');
  // console.log("replacedChinessCap 6 = " + replacedChinessCap);
  replacedChinessCap = replacedChinessCap.replace(/零圆/g, '圆');
  // console.log("replacedChinessCap 7 = " + replacedChinessCap);
  replacedChinessCap = replacedChinessCap.replaceAll('亿萬', '亿');
  // console.log("replacedChinessCap 8 = " + replacedChinessCap);
  if (replacedChinessCap.startsWith('零圆')) {
    replacedChinessCap = replacedChinessCap.replace(/零圆/, '');
  }
  if (replacedChinessCap.startsWith('圆')) {
    replacedChinessCap = replacedChinessCap.replace(/圆/, '');
  }
  if (!replacedChinessCap || replacedChinessCap.length === 0) {
    replacedChinessCap = '零圆';
  }
  return replacedChinessCap;
};
